package dao;

import org.apache.ibatis.annotations.*;
import pojo.Gg;

import java.util.List;

@Mapper
/**
 * 公告表数据访问接口
 */
public interface GgMapper {
    // 根据ID查询公告
    @Select("SELECT * FROM gg WHERE id = #{id}")
    Gg findById(Integer id);

    // 查询所有显示的公告（前台展示用）
    @Select("SELECT * FROM gg WHERE is_show = 1 ORDER BY sort_order DESC, publish_time DESC")
    List<Gg> findPublished();

    // 查询所有公告（后台管理用）
    @Select("SELECT * FROM gg ORDER BY sort_order DESC, publish_time DESC")
    List<Gg> findAll();

    // 新增公告
    @Insert("INSERT INTO gg (title, content, publish_time, is_show, sort_order) " +
            "VALUES (#{title}, #{content}, #{publishTime}, #{isShow}, #{sortOrder})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int add(Gg gg);

    // 更新公告
    @Update("UPDATE gg SET " +
            "title = #{title}, " +
            "content = #{content}, " +
            "publish_time = #{publishTime}, " +
            "is_show = #{isShow}, " +
            "sort_order = #{sortOrder} " +
            "WHERE id = #{id}")
    int update(Gg gg);

    // 删除公告（逻辑删除，设置为不显示）
    @Update("UPDATE gg SET is_show = 0 WHERE id = #{id}")
    int delete(Integer id);
}